package ar.edu.taxi.persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class DAOVehicles extends DataAccesObject implements IfDAO{

	@Override
	public int delete(String condicion) {
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "DELETE FROM vehicles " +
						"WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

	@Override
	public IfDTO find(String codigo) {
		DTOVehicles vehicle = null;
		Connection con = getConexion();
		if(codigo == null || codigo.length() == 0) return null;
		String query = "SELECT * FROM vehicles " +
						"WHERE " + codigo;
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				vehicle = new DTOVehicles(rs.getInt("id"), rs.getString("maker"), rs.getString("model"), 
						rs.getInt("year"), rs.getInt("idVehicleType"), rs.getString("color"), 
						rs.getString("licensePlate"), rs.getString("taxiLicensePlate"), rs.getInt("taxiNumber"));
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return vehicle;
	}

	@Override
	public int insert(IfDTO bean) {
		int numFilas = 0;
		DTOVehicles vehicle = (DTOVehicles) bean;
		Connection con = getConexion();
		String query = "INSERT INTO vehicles VALUES( DEFAULT"+
						", '"+vehicle.getMaker()+ "'" +
						", '"+vehicle.getModel()+ "'" +
						", '"+vehicle.getYear()+ "'" +
						", '"+vehicle.getIdVehicleType()+ "'" +
						", '"+vehicle.getColor()+ "'" +
						", '"+vehicle.getLicensePlate()+ "'" +
						", '"+vehicle.getTaxiLicensePlate()+ "'" +
						", '"+vehicle.getTaxiNumber()+ "'" +
						" )";
		try {
			Statement sentencia = con.createStatement();
			numFilas = sentencia.executeUpdate(query);
			sentencia.close();
			cerrarConexion( con );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return numFilas;
	}

	@Override
	public Vector<IfDTO> select(String condicion) {
		Vector<IfDTO> vehicles = new Vector<IfDTO>();
		Connection con = getConexion();
		String query = "SELECT * FROM vehicles " +
						(condicion == null || condicion.length() == 0 ? "" : "WHERE " + condicion) +
						" ORDER BY id";
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				DTOVehicles vehicle = new DTOVehicles(rs.getInt("id"), rs.getString("maker"), rs.getString("model"), 
						rs.getInt("year"), rs.getInt("idVehicleType"), rs.getString("color"), 
						rs.getString("licensePlate"), rs.getString("taxiLicensePlate"), rs.getInt("taxiNumber"));
				vehicles.add(vehicle);
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return vehicles;
	}

	@Override
	public int update(IfDTO dto, String condicion) {
		DTOVehicles vehicle = (DTOVehicles) dto;
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "UPDATE vehicles SET" +
						" maker='" + vehicle.getMaker() + "'" +
						", model='" + vehicle.getModel() + "'" +
						", year='" + vehicle.getYear() + "'" +
						", idVehicleType='" + vehicle.getIdVehicleType() + "'" +
						", color='" + vehicle.getColor() + "'" +
						", licensePlate='" + vehicle.getLicensePlate() + "'" +
						", taxiLicensePlate='" + vehicle.getTaxiLicensePlate() + "'" +
						", taxiNumber='" + vehicle.getTaxiNumber() + "'" +
						" WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}
}
